//
//  CheckTableViewController.m
//  NaviAndTable
//
//  Created by student on 16/3/5.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "CheckTableViewController.h"

@interface CheckTableViewController ()
@property (nonatomic,strong) NSArray *list;

//记录你选择的那一项
@property (nonatomic,strong) NSIndexPath *lastIndexPath;
@end

@implementation CheckTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.list = @[@"Who Hash",
                  @"Bubba Gump Shrimp Étouffée", @"Who Pudding", @"Scooby Snacks",
                  @"Everlasting Gobstopper", @"Green Eggs and Ham", @"Soylent Green",
                  @"Hard Tack", @"Lembas Bread", @"Roast Beast", @"Blancmange",@"Who Hash",
                  @"Bubba Gump Shrimp Étouffée", @"Who Pudding", @"Scooby Snacks",
                  @"Everlasting Gobstopper", @"Green Eggs and Ham", @"Soylent Green",
                  @"Hard Tack", @"Lembas Bread", @"Roast Beast", @"Blancmange",
                  @"Toy Story",@"A Bug's Life", @"Toy Story 2", @"Monsters, Inc.",
                  @"Finding Nemo", @"The Incredibles", @"Cars",
                  @"Ratatouille", @"WALL-E", @"Up", @"Toy Story 3",
                  @"Cars 2", @"Brave",@"Toy Story",
                  @"A Bug's Life", @"Toy Story 2", @"Monsters, Inc.",
                  @"Finding Nemo", @"The Incredibles", @"Cars",
                  @"Ratatouille", @"WALL-E", @"Up", @"Toy Story 3",
                  @"Cars 2", @"Brave",
                  @"Toy Story",
                  @"A Bug's Life", @"Toy Story 2", @"Monsters, Inc.",
                  @"Finding Nemo", @"The Incredibles", @"Cars",
                  @"Ratatouille", @"WALL-E", @"Up", @"Toy Story 3",
                  @"Cars 2", @"Brave"];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath ];
    cell.textLabel.text = self.list[indexPath.row];
    
    if (self.lastIndexPath != nil && self.lastIndexPath.row == indexPath.row)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //得到上次选中的单元格。取消对勾
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
    lastCell.accessoryType = UITableViewCellAccessoryNone;
    
    
    //得到当前选中状态
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    
    //保存当前选中项
    self.lastIndexPath = indexPath;
    
    //取消表格本身的选中状态
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}



















/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
